package com.whh.cos.service.impl;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.model.*;
import com.whh.cos.model.ObjectInfo;
import com.whh.cos.service.ObjectService;
import com.whh.cos.system.ServiceException;
import com.whh.cos.utils.CosAuth;
import com.whh.cos.utils.DateUtil;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author huahui.wu.
 *         Created on 2018/1/16.
 */
@Service
public class ObjectServiceImpl implements ObjectService {
    private COSClient cosClient;

    public PutObjectResult putObject(String name, String key, File file) throws ServiceException {
        cosClient = CosAuth.getCosAuth();
        return cosClient.putObject(name, key, file);
    }

    public PutObjectResult putObject(String name, String key, InputStream input, ObjectMetadata metadata) throws ServiceException {
        cosClient = CosAuth.getCosAuth();
        return cosClient.putObject(name, key, input, metadata);
    }

    public PutObjectResult putObject(PutObjectRequest putObjectRequest) {
        cosClient = CosAuth.getCosAuth();
        return cosClient.putObject(putObjectRequest);
    }

    @Override
    public List<ObjectInfo> listObjects(String bucketName, String prefix) throws ServiceException {
        cosClient = CosAuth.getCosAuth();
        ObjectListing objectListing = cosClient.listObjects(bucketName, prefix);
        if (objectListing == null) {
            return new ArrayList<>();
        }
        List<COSObjectSummary> objectSummaries = objectListing.getObjectSummaries();
        List<ObjectInfo> objectInfos = new ArrayList<>();
        for (COSObjectSummary cosObjectSummary : objectSummaries) {
            ObjectInfo objectInfo = new ObjectInfo();
            objectInfo.setBucketName(cosObjectSummary.getBucketName());
            objectInfo.setSize(cosObjectSummary.getSize() / 1024 + "KB");
            if (cosObjectSummary.getSize() == 0) {
                continue;
            }
            objectInfo.setLastModified(DateUtil.format(cosObjectSummary.getLastModified(), DateUtil.DATE_TIME_FORMAT));
            objectInfo.setObjectName(getObjectName(cosObjectSummary.getKey(), cosObjectSummary.getSize()));
            objectInfo.setKey(cosObjectSummary.getKey());
            COSObject cosObject = cosClient.getObject(bucketName, cosObjectSummary.getKey());
            if (cosObject != null) {
                objectInfo.setContentType(cosObject.getObjectMetadata().getContentType());
                if (cosObjectSummary.getSize() != 0) {
                    objectInfo.setUrl(cosObject.getObjectContent().getHttpRequest().getURI().toString());
                }
            }
            objectInfos.add(objectInfo);
        }
        return objectInfos;
    }

    public void deleteObject() throws ServiceException {

    }

    @Override
    public ObjectMetadata headObject(String bucketName, String key) throws ServiceException {
        cosClient = CosAuth.getCosAuth();
        COSObject cosObject = cosClient.getObject(bucketName, key);
        if (cosObject == null) {
            return null;
        }
        return cosObject.getObjectMetadata();
    }

    private String getObjectName(String key, Long size) {
        if (size != 0) {
            int end = key.lastIndexOf("/");
            if (end > 0) {
                return key.substring(end + 1, key.length());
            }
        }
        return key;
    }
}
